<template>
  <transition name="slide-fade">
    <div class="alert" v-if="showAlert">
      <div class="wrap">
        <div class="head">
          {{title}}
          <span class="pull-righ close" @click="close">&times;</span>
        </div>
        <div class="body">
          <slot>
            <p>{{message}}</p>
          </slot>
        </div>
        <div class="foot">
          <div v-if="type === 'confirm'">
            <button class="btn-gray" @click="cancel">取消</button>
            <button class="btn-base" @click="sure">{{btnSave}}</button>
          </div>
          <div v-else-if="type === 'inform'">
            <button class="btn-base" @click="cancel">知道了</button>
          </div>
        </div>
      </div>
    </div>
  </transition>
</template>
<script>
export default {
  name: "alert",
  data() {
    return {
      showAlert: false
    };
  },
  props: {
    title: {
      type: String,
      default: "提示"
    },
    btnSave: {
      type: String,
      default: "确定"
    },
    message: {
      type: String
    },
    type: {
      // 可以有confirm, 和inform两个类型
      type: String,
      default: "confirm",
      validator(value) {
        return value === "confirm" || value === "inform";
      }
    },
    sureBtn: {
      type: Function
    },
    cancelBtn: {
      type: Function
    },
    context: {
      type: Object
    }
  },
  methods: {
    cancel() {
      if (this.cancelBtn) {
        this.cancelBtn.apply(this.context);
      }
      this.close();
    },
    sure() {
      if (this.sureBtn) {
        this.sureBtn.apply(this.context);
      }
      this.close();
    },
    show() {
      this.showAlert = true;
    },
    close() {
      this.showAlert = false;
    }
  }
};
</script>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped >
.alert {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 100000;
}

.wrap {
  position: absolute;
  z-index: 100002;
  min-width: 400px;
  background: #fff;
  left: 50%;
  top: 50%;
  border-radius: 4px;
  /* animation: bounce-in 0.5s; */
  transform: translate(-50%, -50%);
  box-shadow: 0 0 5px #ddd;
}

.head {
  height: 40px;
  line-height: 40px;
  border-bottom: 1px solid #dedede;
  padding-left: 10px;
  color: #333;
}
.close {
  font-size: 25px;
  line-height: 40px;
  width: 40px;
  text-align: center;
}
.body {
  padding: 10px 20px;
  min-height: 80px;
}
@keyframes bounce-in {
  0% {
    transform: translateY(-40%);
    opacity: 0;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}
.slide-fade-leave-active {
  transition: all 0.3s cubic-bezier(1, 0.5, 0.8, 1);
}

.slide-fade-enter, .slide-fade-leave-to
  /* .slide-fade-leave-active for below version 2.1.8 */ {
  transform: translateY(-20%);
  opacity: 0;
}
.foot {
  text-align: right;
  padding: 10px 25px;
}
.foot button {
  display: inline-block;
  line-height: 1;
  white-space: nowrap;
  cursor: pointer;
  background: #fff;
  border: 1px solid #c4c4c4;
  color: #1f2d3d;
  margin: 0;
  padding: 10px 15px;
  border-radius: 4px;
}
.foot .btn-base {
  color: #fff;
  background-color: #20a0ff;
  border-color: #20a0ff;
}
.foot .btn-base:hover {
  background: #4db3ff;
  border-color: #4db3ff;
  color: #fff;
}
.foot .btn-gray:hover {
  color: #20a0ff;
  border-color: #20a0ff;
}
.foot button {
  margin-right: 10px;
}
.foot button:last-child {
  margin-right: 0;
}
@media screen and (max-width: 768px) {
  .wrap {
    animation: initial;
  }
}
@media screen and (max-width: 425px) {
  .wrap {
    min-width: 300px;
    left: 50%;
    top: 40%;
    transform: translate(-45%, 0);
  }
}
</style>
